package ru.ognick.coachsystem.manager;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;

import ru.ognick.coachsystem.R;
import ru.ognick.coachsystem.common.Exercise;
import ru.ognick.coachsystem.common.Training;
import ru.ognick.coachsystem.core.CoreActivity;
import ru.ognick.coachsystem.editor.EditorActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;


public class ManagerActivity extends Activity implements OnClickListener {
    public final String TAG = getClass().getSimpleName();
    private final int EDITOR_ACTIVITY_ID = 1001;
    private final int   CORE_ACTIVITY_ID = 1002;
    private ListView listView;
    
    private TrainingAdapter trainingsAdapter;
    private View selectedItem;
    
///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////INITIALIZE///////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.manager);
        listView = (ListView) findViewById(R.id.listViewManager);
        trainingsAdapter = new TrainingAdapter(this, 
        		                                   android.R.layout.simple_list_item_1,
        		                                   new ArrayList<TrainingView>());
        trainingsAdapter.loadStored(ManagerActivity.this);
        listView.setAdapter(trainingsAdapter);
        

    }
    
///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////INTENT CALLBACK//////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
    
    @Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        trainingsAdapter.loadStored(this);
    }
    
///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////CONTEXT MENU/////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
    
	@Override
	public void onClick(View v) {
		selectedItem = v;
		registerForContextMenu(selectedItem);
		selectedItem.showContextMenu();
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
	  super.onCreateContextMenu(menu, v, menuInfo);
 
	  MenuInflater inflater = getMenuInflater();
	  inflater.inflate(R.layout.manager_context_menu, menu);
	}
	
	
	
	private Intent buildIntent(Class<?> cls){
        Intent intent = new Intent(this, cls);
        TrainingView trainingView = (TrainingView) selectedItem.getTag();
        intent.putExtra("TRAINING", trainingView.getTraining());
        return intent;
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
	  switch (item.getItemId()) {
	  case R.id.icm_delete:
		  trainingsAdapter.remove((TrainingView) selectedItem.getTag());
	    return true;
	  case R.id.icm_edit:
        startActivityForResult(buildIntent(EditorActivity.class), EDITOR_ACTIVITY_ID);
	    return true;
	  case R.id.icm_run:
	        startActivityForResult(buildIntent(CoreActivity.class), CORE_ACTIVITY_ID);
		    return true;	    
	  default:
	    return super.onContextItemSelected(item);
	  }
	}    
   
///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////OPTION MENU//////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////

	@Override
	public boolean onCreateOptionsMenu(Menu menu){
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.layout.manager_options_menu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
			case R.id.imm_add:   
				String date = DateFormat.getTimeInstance().format(new Date()).replaceAll(":|\\s", "_");
				Training training = new Training(date);
				for (int i=0 ; i<10; ++i)
					training.getExercises().add(new Exercise());
				View view = trainingsAdapter.add(training).getView(); 
				view.setOnClickListener(ManagerActivity.this);
				training.save();
				return true;
			default:
				return super.onOptionsItemSelected(item);
		}
	}
}